package com.yint.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientTest {

	public static void main(String[] args) {
		String hostname = "127.0.0.1";
		int port = 80;
		System.out.println("Connectingto: " + hostname + ":" + port);
		try {
			//连接到目的地
			Socket client = new Socket(hostname, port);

			PrintWriter out = new PrintWriter(client.getOutputStream());
			BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
			String input;
			//读取输入
			while ((input = stdIn.readLine()) != null) {
				//发送字符串
				out.println(input);// 發送輸入的字符串
				//强制将缓冲区内的字符串输出
				out.flush();
				if (input.equals("exit")) {
					break;
				}
				System.out.println("server:" + in.readLine());
			}
			client.close();
			System.out.println("clientstop.");
		} catch (UnknownHostException e) {
			System.err.println("Don'tknowabouthost:" + hostname);
		} catch (IOException e) {
			System.err.println("Couldn'tgetI/Oforthesocketconnection");
		}

	}

}
